package com.team.easybuy.service.Impl;

import com.team.easybuy.entity.Car;
import com.team.easybuy.entity.Collect;
import com.team.easybuy.service.ColService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ColServiceImpl implements ColService {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public String addCollect(Collect collect, Integer id) {
        if (redisTemplate.hasKey("col")) {
            Map<Integer, List<Collect>> myCollect = (Map<Integer, List<Collect>>) redisTemplate.opsForValue().get("col");
            if (myCollect != null) {
                List<Collect> list = myCollect.get(id);
                if (list != null) {
                    for (Collect col : list) {
                        if (col.getcId().equals(collect.getcId())) {
                            return "已在收藏夹";
                        }
                    }
                    if (list.size() >= 6) {
                        list.remove(0);
                    }
                    list.add(collect);
                    myCollect.put(id, list);
                    redisTemplate.opsForValue().set("col", myCollect);
                } else {
                    List<Collect> colList = new ArrayList<>();
                    colList.add(collect);
                    myCollect.put(id, colList);
                    redisTemplate.opsForValue().set("col", myCollect);
                }
            }
            return "1";
        } else {
            Map<Integer, List<Collect>> map = new HashMap<>();
            List<Collect> list = new ArrayList<>();
            list.add(collect);
            map.put(id, list);
            redisTemplate.opsForValue().set("col", map);
            return "1";
        }
    }

    @Override
    public List<Collect> loadCollectList(Integer id) {
        if (redisTemplate.hasKey("col")) {
            Map<Integer, List<Collect>> myCollect = (Map<Integer, List<Collect>>) redisTemplate.opsForValue().get("col");
            if (myCollect != null) {
                return myCollect.get(id);
            }
        }
        return null;
    }

    @Override
    public boolean delInCol(Integer id, int index) {
        if (redisTemplate.hasKey("col")) {
            Map<Integer, List<Collect>> myCollect = (Map<Integer, List<Collect>>) redisTemplate.opsForValue().get("col");
            if (myCollect != null) {
                List<Collect> list = myCollect.get(id);
                list.remove(index);
                myCollect.put(id, list);
                redisTemplate.opsForValue().set("col", myCollect);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean delAllCol(Integer id) {
        if (redisTemplate.hasKey("col")) {
            Map<Integer, List<Collect>> myCollect = (Map<Integer, List<Collect>>) redisTemplate.opsForValue().get("col");
            if (myCollect != null) {
                List<Collect> list = myCollect.get(id);
                list.clear();
                myCollect.put(id, list);
                redisTemplate.opsForValue().set("col", myCollect);
                return true;
            }
        }
        return false;
    }

}
